﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Text;
using System.Threading.Tasks;

namespace WcfInspectorLib
{
    public class SoapHeaderClientInspector : ClientMessageInspector, IClientMessageInspector, IEndpointBehavior
    {
        public SoapHeaderClientInspector() : this(null, null) { }

        public SoapHeaderClientInspector(string name, string ns) : this(name, ns, null) { }

        public SoapHeaderClientInspector(string name, string ns, string value)
        {
            Name = name;
            NameSpace = ns;
            Value = value;

            BeforeSendRequest += AddSoapHeader;
        }

        private void AddSoapHeader(object sender, BeforeSendRequestEventArgs e)
        {
            if(!string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(NameSpace))
                e.Message.Headers.Add(MessageHeader.CreateHeader(Name, NameSpace, Value));
        }

        public string Name { get; set; }

        public string NameSpace { get; set; }

        public string Value { get; set; }
    }
}
